package com.shilin.lanqiupc.util;

import javazoom.jl.player.Player;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;


/**
 * 音频文件已流的方式读入到内存中，然后内存将这个流交给音频设备，
 * @author Emine Wang
 */
public class PlayVoice extends Thread {

    public static Player player;

    public static void run(String file) {
        if(player!=null){
            player.close();
        }
        try {
            //声明一个File对象
            File mp3 = new File(file);

            //创建一个输入流
            FileInputStream fileInputStream = new FileInputStream(mp3);

            //创建一个缓冲流
            BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);

            //创建播放器对象，把文件的缓冲流传入进去
            player = new Player(bufferedInputStream);

            //调用播放方法进行播放
            player.play();


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}